package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginInfoController {

    @Autowired
    private ILoginInfoService loginInfoService;

    @RequestMapping("/userRegister")
    @ResponseBody
    public AjaxResult userRegister(String username, String verifycode, String password, String confirmPwd){
        AjaxResult result = null;
        try {
            //注册操作
            loginInfoService.userRegister(username,verifycode,password,confirmPwd);
            result = new AjaxResult("注册成功");
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false,e.getMessage());
        }catch (Exception ex){
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false,"系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }

    @RequestMapping("/validateUsername")
    @ResponseBody
    public boolean validateUsername(String username){
        //AjaxResult result = null;
        try {
            //注册操作
            loginInfoService.validateUsername(username);
            //result = new AjaxResult("该手机号码可以注册");
        } catch (DisplayableException e) {
            e.printStackTrace();
            /*//如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false,e.getMessage());*/
            return false;
        }catch (Exception ex){
            ex.printStackTrace();
            /*//把异常信息写入到日志中
            result = new AjaxResult(false,"系统异常,程序员哥哥正在努力抢救中...");*/
            return false;
        }
        return true;
    }
}
